home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / AlteredStates / Source / DrugTest / drugtest.c next >
Encoding:
C/C++ Source or Header  |  1999-06-26  |  17.1 KB  |  651 lines  |  [TEXT/CWIE]

  1. #include<stdio.h>
  2. #include<stdarg.h>
  3. #include<LowMem.h>
  4.  
  5. #pragma mark Globals
  6.  
  7. WindowPtr theWindow;
  8. Boolean gDone=false;
  9. unsigned long gFilter = 0;
  10. GWorldPtr gBraveNewGWorld;
  11.  
  12. #pragma mark Protos
  13.  
  14. void dprintf(char *format, ...);
  15. void InitToolbox(void);
  16. void Invert(Rect *);
  17. void Dim(Rect *);
  18. void Blur(Rect *);
  19. void Apple2(Rect *);
  20. void Flip(Rect *);
  21. void NoBlue(Rect *);
  22. void Trace(Rect *);
  23. void Right(Rect *);
  24. void Left(Rect *);
  25. void DarkBlur(Rect *baz);
  26. void Mouse(Point pt, Rect rect);
  27. void MainLoop(Rect rect);
  28. void ApplyFilters(Rect *baz);
  29. void Menu(unsigned long menuselectinfo);
  30. void MenuStuff(void);
  31. void Intro(void);
  32. void Nostalgia(Rect*);
  33.  
  34. void InitToolbox(void)
  35. {
  36.         InitGraf(&qd.thePort);
  37.         InitCursor();
  38.         InitWindows();
  39.         InitMenus();
  40.         TEInit();
  41.         InitDialogs(nil);
  42.         InitFonts();
  43.         
  44.         FlushEvents(everyEvent, nil);        
  45. }
  46.  
  47. void ApplyFilters(Rect *baz)
  48. {
  49.     int i;
  50.     GWorldPtr temp;
  51.     GDHandle tempGD;
  52.     
  53.     GetGWorld(&temp, &tempGD);
  54.     SetGWorld(gBraveNewGWorld, nil);
  55.     for(i=0;i<32;i++)
  56.     {
  57.         if(gFilter & 1 << i)
  58.         {
  59.             switch(i)
  60.             {
  61.                 case 0:
  62.                     Dim(baz);
  63.                     break;
  64.                 case 1:
  65.                     Trace(baz);
  66.                     break;
  67.                 case 2:
  68.                     Flip(baz);
  69.                     break;
  70.                 case 3:
  71.                     Invert(baz);
  72.                     break;
  73.                 case 4:
  74.                     Blur(baz);
  75.                     break;
  76.                 case 5:
  77.                     Apple2(baz);
  78.                     break;
  79.                 case 6:
  80.                     Right(baz);
  81.                     break;
  82.                 case 7:
  83.                     Left(baz);
  84.                     break;
  85.                 case 8:
  86.                     Nostalgia(baz);
  87.                     break;
  88.                 default:
  89.                     break;
  90.             }
  91.         }
  92.     }
  93.     SetGWorld(temp, tempGD);
  94. }
  95.  
  96. void MainLoop(Rect rect)
  97. {
  98.     EventRecord event;
  99.     CGrafPtr screen;
  100.     Point tempPoint;
  101.     Rect theRect;
  102.     EventRecord theEvent;
  103.     GWorldPtr temp;
  104.     GDHandle tempGD;
  105.     
  106.     while(!gDone)
  107.     {
  108.         if(WaitNextEvent(everyEvent, &event, 0xFFFFFFF, nil))
  109.         {
  110.             switch(event.what)
  111.             {
  112.                 case mouseDown:
  113.                     Mouse(event.where, rect);
  114.                     break;
  115.                 case keyDown:
  116.                     if( (event.modifiers & cmdKey) != 0 ) {
  117.                         Menu( MenuKey(event.message & charCodeMask ) );
  118.                     rect=theWindow->portRect;
  119.                     SetPort(theWindow);
  120.                     tempPoint.h=0;
  121.                     tempPoint.v=0;
  122.                     LocalToGlobal(&tempPoint);
  123.                     OffsetRect(&theRect, tempPoint.h, tempPoint.v);
  124.                     GetCWMgrPort(&screen);
  125.                     HideWindow(theWindow);
  126.                     WaitNextEvent(0, &theEvent, 5, nil);
  127.                     LockPixels(GetGWorldPixMap(gBraveNewGWorld));
  128.                     CopyBits(&((GrafPtr)screen)->portBits, &((GrafPtr)gBraveNewGWorld)->portBits,
  129.                         &theRect, &rect, nil, nil);
  130.                     ShowWindow(theWindow);
  131.                     GetGWorld(&temp, &tempGD);
  132.                     SetGWorld(gBraveNewGWorld, nil);
  133.                       ApplyFilters(&rect);
  134.                       CopyBits(&((GrafPtr)gBraveNewGWorld)->portBits, &((GrafPtr)theWindow)->portBits,
  135.                         &rect, &theWindow->portRect, nil, nil);
  136.                     UnlockPixels(GetGWorldPixMap(gBraveNewGWorld));
  137.                     }
  138.                     break;
  139.                 case updateEvt:
  140.                     BeginUpdate((WindowPtr)event.message);
  141.         /*            HideWindow(theWindow);
  142.                     GetCWMgrPort(&screen);
  143.  
  144.                     CopyBits(&((GrafPtr)screen)->portBits, &((GrafPtr)gBraveNewGWorld)->portBits,
  145.                         &theWindow->portRect, &rect, nil, nil);
  146.                     ShowWindow(theWindow);
  147.                     GetGWorld(&temp, &tempGD);
  148.                     SetGWorld(gBraveNewGWorld, nil);
  149.     
  150.  
  151.                       ApplyFilters(&rect);*/
  152.                       LockPixels(GetGWorldPixMap(gBraveNewGWorld));
  153.                       CopyBits(&((GrafPtr)gBraveNewGWorld)->portBits, &((GrafPtr)theWindow)->portBits,
  154.                         &theWindow->portRect, &rect, nil, nil);
  155.                         UnlockPixels(GetGWorldPixMap(gBraveNewGWorld));
  156.                     EndUpdate((WindowPtr)event.message);
  157.                     InitCursor();
  158.                     break;
  159.             }
  160.         }
  161.         /* CopyBits(&((GrafPtr)gBraveNewGWorld)->portBits, &((GrafPtr)theWindow)->portBits,
  162.               &rect, &rect, nil, nil);  */
  163.     }
  164. }
  165.  
  166. void Mouse(Point pt, Rect rect)
  167. {
  168.     WindowPtr which;
  169.     short where;
  170.     Rect limits;
  171.     Point tempPoint;
  172.     Rect theRect;
  173.     
  174.     GWorldPtr temp;
  175.     CGrafPtr screen;
  176.     GDHandle tempGD;
  177.     EventRecord theEvent;
  178. //    Rect tempRect;
  179.     SetRect(&limits, -32767, -32767, 32767, 32767);
  180.     
  181.     SetRect(&theRect, 0, 0, 240, 240);
  182.     where = FindWindow(pt, &which);
  183.     switch(where)
  184.     {
  185.         case inMenuBar:
  186.             Menu(MenuSelect(pt));
  187.         case inDrag:
  188.             SelectWindow(theWindow);
  189.             DragWindow(theWindow, pt, &limits);
  190.             rect=theWindow->portRect;
  191.             SetPort(theWindow);
  192.             tempPoint.h=0;
  193.             tempPoint.v=0;
  194.             LocalToGlobal(&tempPoint);
  195.             OffsetRect(&theRect, tempPoint.h, tempPoint.v);
  196.             GetCWMgrPort(&screen);
  197.             HideWindow(theWindow);
  198.             WaitNextEvent(0, &theEvent, 5, nil);
  199.             LockPixels(GetGWorldPixMap(gBraveNewGWorld));
  200.             CopyBits(&((GrafPtr)screen)->portBits, &((GrafPtr)gBraveNewGWorld)->portBits,
  201.                 &theRect, &rect, nil, nil);
  202.             ShowWindow(theWindow);
  203.             GetGWorld(&temp, &tempGD);
  204.             SetGWorld(gBraveNewGWorld, nil);
  205.               ApplyFilters(&rect);
  206.               CopyBits(&((GrafPtr)gBraveNewGWorld)->portBits, &((GrafPtr)theWindow)->portBits,
  207.                 &rect, &theWindow->portRect, nil, nil);
  208.             UnlockPixels(GetGWorldPixMap(gBraveNewGWorld));
  209.             break;
  210.         case inContent:
  211.             LockPixels(GetGWorldPixMap(gBraveNewGWorld));
  212.              GetGWorld(&temp, nil);
  213.              SetGWorld(gBraveNewGWorld, nil);
  214.               ApplyFilters(&rect);
  215.                
  216.              CopyBits(&((GrafPtr)gBraveNewGWorld)->portBits, &((GrafPtr)theWindow)->portBits,
  217.                      &rect, &rect, nil, nil);   
  218.             SetGWorld(temp, nil);         
  219.             UnlockPixels(GetGWorldPixMap(gBraveNewGWorld));
  220.             break;
  221.         case inGoAway:
  222.             gDone=true;
  223.             break;
  224.     }
  225. }
  226.  
  227. void Menu(unsigned long result) {
  228.     unsigned long *temp;
  229.     
  230.     result &= 0xFFFF;
  231.     if(result > 2) {
  232.             gFilter ^= 1UL << (result-3);
  233.             CheckItem( GetMenu(128), result, (gFilter & (1UL << (result-3UL))) != 0UL );
  234.     } else if(result == 1) {
  235.             Gestalt('AltS', (long*)&temp);
  236.             if(temp)
  237.                 *temp = gFilter;
  238.             PaintBehind(theWindow, LMGetGrayRgn());
  239.             CalcVisBehind(theWindow, LMGetGrayRgn());
  240.             DrawMenuBar();
  241.     }
  242.     HiliteMenu(0);
  243. }
  244.  
  245. void MenuStuff(void) {
  246.     long *temp;
  247.     long foo;
  248.  
  249.     InsertMenu( GetMenu(128), 0 );
  250.     DrawMenuBar();
  251.  
  252.     Gestalt('AltS', (long*)&temp);
  253.     if(temp)
  254.         gFilter = *temp;
  255.     else
  256.         gFilter = 0;
  257.  
  258.     for(foo = 0; foo < 32; foo++) {
  259.         if( gFilter & 1UL << foo)
  260.             CheckItem( GetMenu(128), foo+3, true );
  261.     }
  262. }
  263.  
  264. void Intro(void)
  265. {
  266.     PicHandle introPic;
  267.     Rect introRect;
  268.     WindowPtr introWindow;
  269.     EventRecord theEvent;
  270.     
  271.     SetRect(&introRect, 0, 0, 548, 229);
  272.     
  273.     introPic=GetPicture(128);
  274.     
  275.     OffsetRect(&introRect, (qd.screenBits.bounds.right/2)-(548/2),  (qd.screenBits.bounds.bottom/2)-(229/2));
  276.     introWindow=NewCWindow(introWindow, &introRect, "\pDrugTest", true, 2, (WindowPtr)-1, true, nil);
  277.     SetPort(introWindow);
  278.     
  279.     SetRect(&introRect, 0, 0, 548, 229);
  280.     DrawPicture(introPic, &introRect);
  281.     
  282.     while(!Button())
  283.         ;
  284.  
  285.     DisposeWindow(introWindow);
  286.     WaitNextEvent(0, &theEvent, 5, nil);
  287.     FlushEvents(everyEvent, nil);
  288. }
  289.  
  290. void main(void) 
  291. {
  292.     CGrafPtr screen;
  293.     GWorldPtr temp;
  294.     GDHandle tempGD;
  295.     Rect rect, windRect;
  296.  
  297.     InitToolbox();
  298.     Intro();
  299.     SetRect(&rect, 0, 0, 240, 240);
  300.     windRect=rect;
  301.     OffsetRect(&windRect, 100, 100);
  302.     GetCWMgrPort(&screen);
  303.     NewGWorld(&gBraveNewGWorld, 32, &rect, nil, nil, nil);
  304.     CopyBits(&((GrafPtr)screen)->portBits, &((GrafPtr)gBraveNewGWorld)->portBits,
  305.                     &windRect, &rect, nil, nil);
  306.     theWindow=NewWindow(theWindow, &windRect, "\pDrugTest", true, documentProc, (WindowPtr)-1, true, nil);
  307.     
  308.     GetGWorld(&temp, &tempGD);
  309.     SetGWorld(gBraveNewGWorld, nil);
  310.     
  311.     LockPixels(GetGWorldPixMap(gBraveNewGWorld));
  312.  
  313.     ApplyFilters(&rect);
  314.  
  315.     CopyBits(&((GrafPtr)gBraveNewGWorld)->portBits, &((GrafPtr)theWindow)->portBits,
  316.                      &rect, &rect, nil, nil);   
  317.  
  318.     MenuStuff();
  319.  
  320.     UnlockPixels(GetGWorldPixMap(gBraveNewGWorld));
  321.     
  322.     while(!gDone)
  323.         MainLoop(rect);
  324.  
  325.     DisposeWindow(theWindow);
  326.     DisposeGWorld(gBraveNewGWorld);
  327.     SetGWorld(temp, tempGD);
  328.     
  329.     FlushEvents(everyEvent, nil);  
  330. }
  331.  
  332. void Dim(Rect *baz) {
  333.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  334.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  335.     Rect foo = bar[0]->bounds;
  336.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  337.     long x, y;
  338.     unsigned long *pixelptr;
  339.  
  340.     if(!data) {
  341.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  342.         return;
  343.     }
  344.  
  345.     for(y = baz->top; y < baz->bottom; y++) {
  346.         for(x = baz->left; x < baz->right; x++) {
  347.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  348.             *pixelptr -= (*pixelptr & 0xF0F0F0F0) >> 4;
  349. //            *(unsigned long*)(data + (y * rbytes) + (x * 4)) >>= 1;
  350.         }
  351.     }
  352. }
  353.  
  354. void Blur(Rect *baz) {
  355.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  356.     unsigned long rbytes = (bar[0]->rowBytes & 0x3FFF) >> 2;
  357.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  358.     long x, y;
  359.     unsigned long *pixelptr;
  360.  
  361.     if(!data) {
  362.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  363.         return;
  364.     }
  365.  
  366.     pixelptr = (unsigned long*)(data + (baz->top * (rbytes<<2)) + (baz->left * 4));
  367.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  368.         ( ( (*(pixelptr + 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  369.         ( ( (*(pixelptr + rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  370.  
  371.     pixelptr = (unsigned long*)(data + ((baz->bottom-1) * (rbytes<<2)) + (baz->left * 4));
  372.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  373.         ( ( (*(pixelptr + 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  374.         ( ( (*(pixelptr - rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  375.  
  376.     pixelptr = (unsigned long*)(data + (baz->top * (rbytes<<2)) + ((baz->right-1) * 4));
  377.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  378.         ( ( (*(pixelptr - 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  379.         ( ( (*(pixelptr + rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  380.  
  381.     pixelptr = (unsigned long*)(data + ((baz->bottom-1) * (rbytes<<2)) + ((baz->right-1) * 4));
  382.     *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  383.         ( ( (*(pixelptr - 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  384.         ( ( (*(pixelptr - rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  385.  
  386.     for(x = baz->left + 1; x < baz->right - 1; x++) {
  387.         pixelptr = (unsigned long*)(data + (baz->top * (rbytes<<2)) + (x * 4));
  388.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  389.             ( ( (*(pixelptr - 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  390.             ( ( (*(pixelptr + 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  391.             ( ( (*(pixelptr + rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  392.  
  393.         pixelptr = (unsigned long*)(data + ((baz->bottom-1) * (rbytes<<2)) + (x * 4));
  394.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  395.             ( ( (*(pixelptr - 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  396.             ( ( (*(pixelptr + 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  397.             ( ( (*(pixelptr - rbytes) & 0xFCFCFCFC) | 0x04040404) >> 2);
  398.     }
  399.  
  400.     for(y = baz->top + 1; y < baz->bottom - 1; y++) {
  401.         pixelptr = (unsigned long*)(data + (y * (rbytes<<2)) + (baz->left * 4));
  402.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  403.             ( ( (*(pixelptr + 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  404.             ( ( (*(pixelptr - rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  405.             ( ( (*(pixelptr + rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3);
  406.  
  407.         pixelptr = (unsigned long*)(data + (y * (rbytes<<2)) + ((baz->right-1) * 4));
  408.         *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  409.             ( ( (*(pixelptr - 1) & 0xFCFCFCFC) | 0x04040404) >> 2) +
  410.             ( ( (*(pixelptr - rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  411.             ( ( (*(pixelptr + rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3);
  412.     }
  413.  
  414.     for(y = baz->top + 1; y < baz->bottom - 1; y++) {
  415.         for(x = baz->left + 1; x < baz->right - 1; x++) {
  416.             pixelptr = (unsigned long*)(data + (y * (rbytes<<2)) + (x * 4));
  417.             *pixelptr = ((*pixelptr & 0xFEFEFEFE) >> 1) +
  418.                 ( ( (*(pixelptr - 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  419.                 ( ( (*(pixelptr + 1) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  420.                 ( ( (*(pixelptr - rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3) +
  421.                 ( ( (*(pixelptr + rbytes) & 0xF8F8F8F8) | 0x08080808) >> 3);
  422.         }
  423.     }
  424. }
  425.  
  426. void Apple2(Rect *baz) {
  427.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  428.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  429.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  430.     long x, y;
  431.     unsigned long *pixelptr;
  432.     unsigned short quux;
  433.  
  434.     if(!data) {
  435.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  436.         return;
  437.     }
  438.  
  439.     for(y = baz->top; y < baz->bottom; y++) {
  440.         for(x = baz->left; x < baz->right; x++) {
  441.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  442.             quux = ((unsigned char*)pixelptr)[1];
  443.             quux += ((unsigned char*)pixelptr)[3];
  444.             quux >>= 1;
  445.             ((unsigned char*)pixelptr)[1] = ((unsigned char*)pixelptr)[3] = quux;
  446.         }
  447.     }
  448. }
  449.  
  450. void Flip(Rect *baz) {
  451.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  452.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  453.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  454.     long x, y;
  455.     unsigned long *top, *bottom;
  456.     unsigned long temp;
  457.  
  458.     if(!data) {
  459.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  460.         return;
  461.     }
  462.  
  463.     for(y = baz->top; y < baz->bottom - ((baz->bottom - baz->top + 1)>>1); y++) {
  464.         for(x = baz->left; x < baz->right; x++) {
  465.             top = (unsigned long*)(data + (y * rbytes) + (x * 4));
  466.             bottom = (unsigned long*)(data + ((baz->bottom - y - 1) * rbytes) + (x * 4));
  467.             temp = *bottom;
  468.             *bottom = *top;
  469.             *top = temp;
  470.         }
  471.     }
  472. }
  473.  
  474. void NoBlue(Rect *baz) {
  475.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  476.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  477.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  478.     long x, y;
  479.     unsigned long *pixelptr;
  480.     short temp;
  481.  
  482.     if(!data) {
  483.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  484.         return;
  485.     }
  486.  
  487.     for(y = baz->top; y < baz->bottom; y++) {
  488.         for(x = baz->left; x < baz->right; x++) {
  489.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  490.             temp = ((unsigned char*)pixelptr)[1] + ((unsigned char*)pixelptr)[2];
  491.             ((unsigned char*)pixelptr)[3] = temp>>1;
  492.         }
  493.     }
  494. }
  495.  
  496. void Trace(Rect *baz) {
  497.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  498.     unsigned long rbytes = (bar[0]->rowBytes & 0x3FFF)>>2;
  499.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  500.     long x, y;
  501.     unsigned long *from, *to;
  502.     unsigned long comp;
  503.     unsigned long *buffer = (unsigned long *)NewPtr( (baz->right - baz->left) * (baz->bottom - baz->top) * 4 );
  504.  
  505.     if(!data) {
  506.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  507.         return;
  508.     }
  509.  
  510.     if(!buffer) {
  511.         DebugStr("\pthe buffer is nil");
  512.         return;
  513.     }
  514.  
  515.     for(y = baz->top; y < baz->bottom; y++) {
  516.         for(x = baz->left; x < baz->right; x++) {
  517.             from = (unsigned long*)(data + (y * (rbytes<<2)) + (x * 4));
  518.             to = buffer + y*(baz->right - baz->left) + x;
  519.             
  520.             comp =
  521.                 ( ( *(from - 1) & 0xFCFCFCFC) >> 2) +
  522.                 ( ( *(from + 1) & 0xFCFCFCFC) >> 2) +
  523.                 ( ( *(from - rbytes) & 0xFCFCFCFC) >> 2) +
  524.                 ( ( *(from + rbytes) & 0xFCFCFCFC) >> 2);
  525.  
  526.             if( ((char*)from)[1] > ((char*)&comp)[1] )
  527.                 ((char*)to)[1] = ((char*)from)[1] - ((char*)&comp)[1];
  528.             else
  529.                 ((char*)to)[1] = ((char*)&comp)[1] - ((char*)from)[1];
  530.  
  531.             if( ((char*)from)[2] > ((char*)&comp)[2] )
  532.                 ((char*)to)[2] = ((char*)from)[2] - ((char*)&comp)[2];
  533.             else
  534.                 ((char*)to)[2] = ((char*)&comp)[2] - ((char*)from)[2];
  535.  
  536.             if( ((char*)from)[3] > ((char*)&comp)[3] )
  537.                 ((char*)to)[3] = ((char*)from)[3] - ((char*)&comp)[3];
  538.             else
  539.                 ((char*)to)[3] = ((char*)&comp)[3] - ((char*)from)[3];
  540.         }
  541.     }
  542.     for(y = baz->top; y < baz->bottom; y++) {
  543.         for(x = baz->left; x < baz->right; x++) {
  544.             *(unsigned long*)(data + (y * (rbytes<<2)) + (x * 4)) = buffer[y*(baz->right - baz->left) + x];
  545.         }
  546.     }
  547.     DisposePtr((char*)buffer);
  548. }
  549. /*
  550.  * DebugStr() + printf() = very useful function
  551.  */
  552. void dprintf(char *format, ...) {
  553.     va_list args;
  554.     char dbugstr[256];
  555.  
  556.     va_start(args, format);
  557.     vsprintf(dbugstr, format, args);
  558.     va_end(args);
  559.  
  560.     DebugStr(CtoPstr((char*)dbugstr));
  561. }
  562.  
  563. void Invert(Rect *foo) {
  564.     InvertRect(foo);
  565. }
  566.  
  567. void Right(Rect *baz) {
  568.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  569.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  570.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  571.     long x, y;
  572.     unsigned long *pixelptr;
  573.     char foo;
  574.  
  575.     if(!data) {
  576.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  577.         return;
  578.     }
  579.  
  580.     for(y = baz->top; y < baz->bottom; y++) {
  581.         for(x = baz->left; x < baz->right; x++) {
  582.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  583.             foo = ((char*)pixelptr)[1];
  584.             ((char*)pixelptr)[1] = ((char*)pixelptr)[2];
  585.             ((char*)pixelptr)[2] = ((char*)pixelptr)[3];
  586.             ((char*)pixelptr)[3] = foo;
  587.         }
  588.     }
  589. }
  590.  
  591. void Left(Rect *baz) {
  592.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  593.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  594.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  595.     long x, y;
  596.     unsigned long *pixelptr;
  597.     char foo;
  598.  
  599.     if(!data) {
  600.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  601.         return;
  602.     }
  603.  
  604.     for(y = baz->top; y < baz->bottom; y++) {
  605.         for(x = baz->left; x < baz->right; x++) {
  606.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  607.             foo = ((char*)pixelptr)[3];
  608.             ((char*)pixelptr)[3] = ((char*)pixelptr)[2];
  609.             ((char*)pixelptr)[2] = ((char*)pixelptr)[1];
  610.             ((char*)pixelptr)[1] = foo;
  611.         }
  612.     }
  613. }
  614. void Nostalgia(Rect *baz) {
  615.     PixMapHandle bar = GetGWorldPixMap(gBraveNewGWorld);
  616.     unsigned long rbytes = bar[0]->rowBytes & 0x3FFF;
  617.     Ptr data = GetPixBaseAddr(GetGWorldPixMap(gBraveNewGWorld));
  618.     long x, y;
  619.     unsigned long *pixelptr;
  620.  
  621.     if(!data) {
  622.         DebugStr("\pthe pixbaseaddr of our gworld is nil");
  623.         return;
  624.     }
  625.  
  626.     for(y = baz->top; y < baz->bottom; y++) {
  627.         for(x = baz->left; x < baz->right; x++) {
  628.             long avg;
  629.             unsigned long tempColor;
  630.             unsigned char *tempColorPtr;
  631.             pixelptr = (unsigned long*)(data + (y * rbytes) + (x * 4));
  632.             avg = (((unsigned char*)pixelptr)[1] +
  633.                   ((unsigned char*)pixelptr)[2] +
  634.                   ((unsigned char*)pixelptr)[3]) / 3;
  635.             
  636.             avg *= 2;
  637.             
  638.             if (avg > 255) avg = 255;
  639.             else if (avg < 0 ) avg = 0;
  640.             
  641.             tempColorPtr = (unsigned char*)&tempColor;
  642.             tempColorPtr[0] = 0;
  643.             tempColorPtr[1] = avg;
  644.             tempColorPtr[2] = avg * 0.8;
  645.             tempColorPtr[3] = avg * 0.44;
  646.             
  647.             *pixelptr = tempColor;
  648.         }
  649.     }
  650. }
  651.